from django.contrib import admin
from .models import UserChatRoom, UserChatMessage


@admin.register(UserChatRoom)
class UserChatRoomAdmin(admin.ModelAdmin):
    list_display = ['id', 'room_type', 'get_participants', 'created_at', 'updated_at']
    list_filter = ['room_type', 'created_at']
    search_fields = ['participants__username']
    readonly_fields = ['created_at', 'updated_at']

    def get_participants(self, obj):
        return ", ".join([user.username for user in obj.participants.all()])
    get_participants.short_description = '参与者'


@admin.register(UserChatMessage)
class UserChatMessageAdmin(admin.ModelAdmin):
    list_display = ['id', 'room', 'sender', 'message_type', 'content_preview', 'timestamp']
    list_filter = ['message_type', 'timestamp', 'room__room_type']
    search_fields = ['sender__username', 'content', 'room__id']
    readonly_fields = ['timestamp']

    def content_preview(self, obj):
        return obj.content[:50] + '...' if len(obj.content) > 50 else obj.content
    content_preview.short_description = '消息预览'